﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;
using System.Security.Cryptography;
using System.IO;

namespace MarginTrading.Forms
{
    public partial class DBConnectionSettingsDlg : Form
    {
        public DBConnectionSettingsDlg()
        {
            InitializeComponent();
        }

        private void DBConnectionSettingsDlg_Load(object sender, EventArgs e)
        {
            textBoxHost.Text = "localhost";
            textBoxServiceName.Text = "XE";
            textBoxPort.Text = "1521";

            try
            {
                using (StreamReader sr = new StreamReader("orasettings"))
                {
                    string settings = DESEncryptor.Decrypt(sr.ReadToEnd());

                    string[] array = settings.Split(' ');

                    //string settings = textBoxUser.Text + Environment.NewLine + textBoxPass.Text + Environment.NewLine + textBoxHost.Text + Environment.NewLine + textBoxPort.Text + Environment.NewLine + textBoxServiceName.Text;

                    string user = array[0];

                    if (user == null)
                    {
                        return;
                    }

                    string pass = array[1];

                    if (pass == null)
                    {
                        return;
                    }

                    string host = array[2];

                    if (host == null)
                    {
                        return;
                    }

                    string port = array[3];

                    if (port == null)
                    {
                        return;
                    }

                    string servicename = array[4];

                    if (servicename == null)
                    {
                        return;
                    }

                    textBoxHost.Text = host;
                    textBoxPass.Text = pass;
                    textBoxUser.Text = user;
                    textBoxServiceName.Text = servicename;
                    textBoxPort.Text = port;
                }
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (ValidateInputs() == false)
            {
                return;
            }

            //string sConString = "user id=" + textBoxUser.Text + ";password=" + textBoxPass.Text + ";data source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = " + textBoxHost.Text + ")(PORT = " + textBoxPort.Text + "))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = " + textBoxServiceName.Text + ")))";

            string settings = textBoxUser.Text + " " + textBoxPass.Text + " " + textBoxHost.Text + " " + textBoxPort.Text + " " + textBoxServiceName.Text;
            string ciphertext = DESEncryptor.Encrypt(settings);

            using (StreamWriter outfile = new StreamWriter("orasettings"))
            {
                outfile.Write(ciphertext);
            }
            Program.LoadConnectionString();
            Close();
        }

        private bool ValidateInputs()
        {
            if (textBoxHost.Text == ""
                || textBoxPass.Text == ""
                || textBoxPort.Text == ""
                || textBoxServiceName.Text == ""
                || textBoxUser.Text == "")
            {
                MessageBox.Show("Please enter the required details", "Required fields empty", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            int port = 0;
            if (int.TryParse(textBoxPort.Text, out port) == false)
            {
                MessageBox.Show("Invalid port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }
    }
}
